package betting.manager;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import betting.db.entitites.BasketRow;
import betting.db.entitites.SoccerRow;

public class XMLManager {

	public static String BASKET_NBA_FILE = "c:\\Users\\Francesco\\Documents\\databases\\nba.xml";
	public static String SOCCER_SERIEA_FILE = "c:\\Users\\Francesco\\Documents\\databases\\serieA.xml";
	public static String SOCCER_BUNDESLIGA_FILE = "c:\\Users\\Francesco\\Documents\\databases\\bundesliga.xml";
	public static String SOCCER_PREMIERLEAGUE_FILE = "c:\\Users\\Francesco\\Documents\\databases\\premierLeague.xml";
	public static String SOCCER_LIGA_FILE = "c:\\Users\\Francesco\\Documents\\databases\\liga.xml";
	public static String SOCCER_LIGUE1_FILE = "c:\\Users\\Francesco\\Documents\\databases\\ligue1.xml";
	public static String SOCCER_EREDIVISE_FILE = "c:\\Users\\Francesco\\Documents\\databases\\eredivise.xml";
	public static String SOCCER_SCOTTISHPREMIERLEAGUE_FILE = "c:\\Users\\Francesco\\Documents\\databases\\scottishPremierLeague.xml";
	public static String SOCCER_LIGAZONSAGRES_FILE = "c:\\Users\\Francesco\\Documents\\databases\\ligaZonSagres.xml";
	
	
	private static final String BASKET_NBA_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/g/8371/Money-Line.html";
	private static final String SOCCER_SERIEA_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/321/Serie-A.html";
	private static final String SOCCER_LIGA_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/338/Spagna-Liga.html";
	private static final String SOCCER_PREMIERLEAGUE_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/295/Inghilterra-Premier-League.html";
	private static final String SOCCER_BUNDESLIGA_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/315/Germania-Bundesliga.html";
	private static final String SOCCER_EREDIVISE_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/306/Olanda-Eredivisie.html";
	private static final String SOCCER_LIGUE1_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/312/Francia-Ligue-1.html";
	private static final String SOCCER_LIGAZONSAGRES_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/331/Portogallo-Super-Liga.html";
	private static final String SOCCER_SCOTTISHPREMIERLEAGUE_WILLIAM_HILL_LINK = "http://sports.williamhill.it/bet_ita/it/betting/t/297/Scozia-Premiership.html";
	
	
	public static void main(String[] args){
		
		Update_NBA_WH_File();
		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_SERIEA_FILE, SOCCER_SERIEA_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_BUNDESLIGA_FILE, SOCCER_BUNDESLIGA_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_PREMIERLEAGUE_FILE, SOCCER_PREMIERLEAGUE_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_LIGA_FILE, SOCCER_LIGA_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_LIGUE1_FILE, SOCCER_LIGUE1_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_EREDIVISE_FILE, SOCCER_EREDIVISE_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_SCOTTISHPREMIERLEAGUE_FILE, SOCCER_SCOTTISHPREMIERLEAGUE_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
//		
//		Update_Soccer_WH_File(SOCCER_LIGAZONSAGRES_FILE, SOCCER_LIGAZONSAGRES_WILLIAM_HILL_LINK);
//		System.out.println("______________________________________________");
		
	}
	
	public static void Update_NBA_WH_File(){
		try{
			SAXBuilder builder = new SAXBuilder();
			File xmlFile = new File(BASKET_NBA_FILE);
	 
			Document doc = (Document) builder.build(xmlFile);
			
			Element rootNode = doc.getRootElement();
	 
			rootNode.setName("matches");
			
			ArrayList<BasketRow> today = GetScores.getTodayNBAMatches(BASKET_NBA_WILLIAM_HILL_LINK);

			for(int u = 0; u < today.size(); u++){
				
				Element todayMatch = createNBAElement(today.get(u));
				rootNode.addContent(todayMatch);
				
			}
			
			XMLOutputter xmlOutput = new XMLOutputter();
			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(doc, new FileWriter(BASKET_NBA_FILE));
	 
			System.out.println("File updated!");
		  } catch (IOException io) {
			io.printStackTrace();
		  } catch (JDOMException e) {
			e.printStackTrace();
		  }
	}
	
	
	
	public static void Update_Soccer_WH_File(String file_position, String source_link){
		try{
			SAXBuilder builder = new SAXBuilder();
			File xmlFile = new File(file_position);
	 
			Document doc = (Document) builder.build(xmlFile);
			
			Element rootNode = doc.getRootElement();
			rootNode.setName("matches");
			
			ArrayList<SoccerRow> today = GetScores.getTodaySoccerMatches(source_link);

			for(int u = 0; u < today.size(); u++){
				
				Element todayMatch = createSoccerElement(today.get(u));
				rootNode.addContent(todayMatch);
				
			}
			
			XMLOutputter xmlOutput = new XMLOutputter();
	 
			xmlOutput.setFormat(Format.getPrettyFormat());
			xmlOutput.output(doc, new FileWriter(file_position));
	 
			System.out.println("File updated!");
		  } catch (IOException io) {
			io.printStackTrace();
		  } catch (JDOMException e) {
			e.printStackTrace();
		  }
	}
	
	public static Element createNBAElement(BasketRow tablerow){
		
		Element e = new Element("nba");
		Element home = new Element("HOME");
		Element away = new Element("AWAY");
		Element quota1 = new Element("QUOTA1");
		Element quota2 = new Element("QUOTA2");
		Element result = new Element("RESULT");
		Element date = new Element("DATE");
		Element period = new Element("PERIOD");
	
		home.addContent(tablerow.getHome());
		away.addContent(tablerow.getAway());
		
		quota1.addContent(String.valueOf(tablerow.getQuota1()).replaceAll("\\.", "\\,"));
		quota2.addContent(String.valueOf(tablerow.getQuota2()).replaceAll("\\.", "\\,"));
		result.addContent("");
		date.addContent(tablerow.getDate());
		period.addContent(tablerow.getPeriod());
	
		ArrayList<Element> list = new ArrayList<Element>();
		
		list.add(home);
		list.add(away);
		list.add(quota1);
		list.add(quota2);
		list.add(result);
		list.add(date);
		list.add(period);
		
		e.setContent(list);
		return e;
	}
	
	
public static Element createSoccerElement(SoccerRow tablerow){
		
		Element e = new Element("SOCCER");
		Element home = new Element("HOME");
		Element away = new Element("AWAY");
		Element quota1 = new Element("QUOTA1");
		Element quotaX = new Element("QUOTAX");
		Element quota2 = new Element("QUOTA2");
		Element goalhome = new Element("GOAL_HOME");
		Element goalaway = new Element("GOAL_AWAY");
		Element date = new Element("DATE");
		Element league = new Element("LEAGUE");
	
		home.addContent(tablerow.getHome());
		away.addContent(tablerow.getAway());
		
		quota1.addContent(String.valueOf(tablerow.getQuota1()).replaceAll("\\.", "\\,"));
		quota2.addContent(String.valueOf(tablerow.getQuota2()).replaceAll("\\.", "\\,"));
		quotaX.addContent(String.valueOf(tablerow.getQuotaX()).replaceAll("\\.", "\\,"));
		
		goalaway.addContent("");
		goalhome.addContent("");
		
		league.addContent(tablerow.getLeague());
		date.addContent(tablerow.getDate());
	
		ArrayList<Element> list = new ArrayList<Element>();
		
		list.add(home);
		list.add(away);
		list.add(quota1);
		list.add(quotaX);
		list.add(quota2);
		list.add(goalaway);
		list.add(goalhome);
		list.add(date);
		list.add(league);
		
		e.setContent(list);
		return e;
	}
}
